/**
 * 权限代码枚举
 * 统一管理所有权限定义，便于维护和引用
 */

// 权限模块分组
export const PERMISSION_MODULES = {
  USER: 'user',
  ROLE: 'role', 
  PERMISSION: 'permission',
  SYSTEM: 'system',
  LOG: 'log',
  ADMIN: 'admin',
} as const;

// 权限操作类型
export const PERMISSION_ACTIONS = {
  LIST: 'list',
  READ: 'read',
  CREATE: 'create',
  UPDATE: 'update',
  DELETE: 'delete',
  EXPORT: 'export',
  IMPORT: 'import',
  APPROVE: 'approve',
  REJECT: 'reject',
  PUBLISH: 'publish',
  UNPUBLISH: 'unpublish',
  ENABLE: 'enable',
  DISABLE: 'disable',
  RESET: 'reset',
  BACKUP: 'backup',
  RESTORE: 'restore',
  CONFIGURE: 'configure',
  MONITOR: 'monitor',
  ANALYZE: 'analyze',
  ACCESS: 'access',
} as const;

// 权限资源类型
export const PERMISSION_RESOURCES = {
  USER: 'user',
  ROLE: 'role',
  PERMISSION: 'permission',
  CONFIG: 'config',
  STATS: 'stats',
  LOG: 'log',
  SYSTEM: 'system',
  ADMIN: 'admin',
  PROFILE: 'profile',
  SETTING: 'setting',
  NOTIFICATION: 'notification',
  REPORT: 'report',
  DASHBOARD: 'dashboard',
} as const;

// 动态生成权限代码
export const generatePermissionCode = (resource: string, action: string): string => {
  return `${resource}:${action}`;
};

// 所有权限汇总
export const ALL_PERMISSIONS = {
  // 用户管理权限
  USER_LIST: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.LIST),
  USER_READ: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.READ),
  USER_CREATE: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.CREATE),
  USER_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.UPDATE),
  USER_DELETE: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.DELETE),
  USER_ENABLE: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.ENABLE),
  USER_DISABLE: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.DISABLE),
  USER_RESET: generatePermissionCode(PERMISSION_RESOURCES.USER, PERMISSION_ACTIONS.RESET),

  // 角色管理权限
  ROLE_READ: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.READ),
  ROLE_CREATE: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.CREATE),
  ROLE_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.UPDATE),
  ROLE_DELETE: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.DELETE),
  ROLE_ENABLE: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.ENABLE),
  ROLE_DISABLE: generatePermissionCode(PERMISSION_RESOURCES.ROLE, PERMISSION_ACTIONS.DISABLE),

  // 权限管理权限
  PERMISSION_READ: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.READ),
  PERMISSION_CREATE: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.CREATE),
  PERMISSION_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.UPDATE),
  PERMISSION_DELETE: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.DELETE),
  PERMISSION_ENABLE: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.ENABLE),
  PERMISSION_DISABLE: generatePermissionCode(PERMISSION_RESOURCES.PERMISSION, PERMISSION_ACTIONS.DISABLE),

  // 系统管理权限
  SYSTEM_CONFIG_READ: generatePermissionCode(PERMISSION_RESOURCES.CONFIG, PERMISSION_ACTIONS.READ),
  SYSTEM_CONFIG_CREATE: generatePermissionCode(PERMISSION_RESOURCES.CONFIG, PERMISSION_ACTIONS.CREATE),
  SYSTEM_CONFIG_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.CONFIG, PERMISSION_ACTIONS.UPDATE),
  SYSTEM_CONFIG_DELETE: generatePermissionCode(PERMISSION_RESOURCES.CONFIG, PERMISSION_ACTIONS.DELETE),
  SYSTEM_CONFIG_CONFIGURE: generatePermissionCode(PERMISSION_RESOURCES.CONFIG, PERMISSION_ACTIONS.CONFIGURE),
  SYSTEM_STATS_READ: generatePermissionCode(PERMISSION_RESOURCES.STATS, PERMISSION_ACTIONS.READ),
  SYSTEM_STATS_ANALYZE: generatePermissionCode(PERMISSION_RESOURCES.STATS, PERMISSION_ACTIONS.ANALYZE),
  SYSTEM_MONITOR: generatePermissionCode(PERMISSION_RESOURCES.SYSTEM, PERMISSION_ACTIONS.MONITOR),
  SYSTEM_BACKUP: generatePermissionCode(PERMISSION_RESOURCES.SYSTEM, PERMISSION_ACTIONS.BACKUP),
  SYSTEM_RESTORE: generatePermissionCode(PERMISSION_RESOURCES.SYSTEM, PERMISSION_ACTIONS.RESTORE),

  // 日志管理权限
  LOG_READ: generatePermissionCode(PERMISSION_RESOURCES.LOG, PERMISSION_ACTIONS.READ),
  LOG_DELETE: generatePermissionCode(PERMISSION_RESOURCES.LOG, PERMISSION_ACTIONS.DELETE),
  LOG_EXPORT: generatePermissionCode(PERMISSION_RESOURCES.LOG, PERMISSION_ACTIONS.EXPORT),
  LOG_ANALYZE: generatePermissionCode(PERMISSION_RESOURCES.LOG, PERMISSION_ACTIONS.ANALYZE),
  LOG_STATS_READ: generatePermissionCode(PERMISSION_RESOURCES.STATS, PERMISSION_ACTIONS.READ),

  // 后台管理权限
  ADMIN_ACCESS: generatePermissionCode(PERMISSION_RESOURCES.ADMIN, PERMISSION_ACTIONS.ACCESS),
  ADMIN_DASHBOARD_READ: generatePermissionCode(PERMISSION_RESOURCES.DASHBOARD, PERMISSION_ACTIONS.READ),
  ADMIN_REPORT_READ: generatePermissionCode(PERMISSION_RESOURCES.REPORT, PERMISSION_ACTIONS.READ),
  ADMIN_REPORT_EXPORT: generatePermissionCode(PERMISSION_RESOURCES.REPORT, PERMISSION_ACTIONS.EXPORT),
  ADMIN_NOTIFICATION_READ: generatePermissionCode(PERMISSION_RESOURCES.NOTIFICATION, PERMISSION_ACTIONS.READ),
  ADMIN_NOTIFICATION_CREATE: generatePermissionCode(PERMISSION_RESOURCES.NOTIFICATION, PERMISSION_ACTIONS.CREATE),
  ADMIN_SETTING_READ: generatePermissionCode(PERMISSION_RESOURCES.SETTING, PERMISSION_ACTIONS.READ),
  ADMIN_SETTING_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.SETTING, PERMISSION_ACTIONS.UPDATE),

  // 个人权限
  PROFILE_READ: generatePermissionCode(PERMISSION_RESOURCES.PROFILE, PERMISSION_ACTIONS.READ),
  PROFILE_UPDATE: generatePermissionCode(PERMISSION_RESOURCES.PROFILE, PERMISSION_ACTIONS.UPDATE),
  PROFILE_RESET_PASSWORD: generatePermissionCode(PERMISSION_RESOURCES.PROFILE, 'reset_password'),
} as const;

// 权限类型定义
export type AllPermission = typeof ALL_PERMISSIONS[keyof typeof ALL_PERMISSIONS];

// 权限模块类型
export type PermissionModule = typeof PERMISSION_MODULES[keyof typeof PERMISSION_MODULES];
export type PermissionAction = typeof PERMISSION_ACTIONS[keyof typeof PERMISSION_ACTIONS];
export type PermissionResource = typeof PERMISSION_RESOURCES[keyof typeof PERMISSION_RESOURCES];

// 权限名称映射
export const PERMISSION_NAMES: Record<string, string> = {
  [ALL_PERMISSIONS.USER_LIST]: '用户列表',
  [ALL_PERMISSIONS.USER_READ]: '用户查看',
  [ALL_PERMISSIONS.USER_CREATE]: '用户创建',
  [ALL_PERMISSIONS.USER_UPDATE]: '用户编辑',
  [ALL_PERMISSIONS.USER_DELETE]: '用户删除',
  [ALL_PERMISSIONS.USER_ENABLE]: '用户启用',
  [ALL_PERMISSIONS.USER_DISABLE]: '用户禁用',
  [ALL_PERMISSIONS.USER_RESET]: '用户重置',

  [ALL_PERMISSIONS.ROLE_READ]: '角色查看',
  [ALL_PERMISSIONS.ROLE_CREATE]: '角色创建',
  [ALL_PERMISSIONS.ROLE_UPDATE]: '角色编辑',
  [ALL_PERMISSIONS.ROLE_DELETE]: '角色删除',
  [ALL_PERMISSIONS.ROLE_ENABLE]: '角色启用',
  [ALL_PERMISSIONS.ROLE_DISABLE]: '角色禁用',

  [ALL_PERMISSIONS.PERMISSION_READ]: '权限查看',
  [ALL_PERMISSIONS.PERMISSION_CREATE]: '权限创建',
  [ALL_PERMISSIONS.PERMISSION_UPDATE]: '权限编辑',
  [ALL_PERMISSIONS.PERMISSION_DELETE]: '权限删除',
  [ALL_PERMISSIONS.PERMISSION_ENABLE]: '权限启用',
  [ALL_PERMISSIONS.PERMISSION_DISABLE]: '权限禁用',

  [ALL_PERMISSIONS.SYSTEM_CONFIG_READ]: '系统配置查看',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CREATE]: '系统配置创建',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_UPDATE]: '系统配置编辑',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_DELETE]: '系统配置删除',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CONFIGURE]: '系统配置设置',
  [ALL_PERMISSIONS.SYSTEM_STATS_READ]: '系统统计查看',
  [ALL_PERMISSIONS.SYSTEM_STATS_ANALYZE]: '系统统计分析',
  [ALL_PERMISSIONS.SYSTEM_MONITOR]: '系统监控',
  [ALL_PERMISSIONS.SYSTEM_BACKUP]: '系统备份',
  [ALL_PERMISSIONS.SYSTEM_RESTORE]: '系统恢复',

  [ALL_PERMISSIONS.LOG_READ]: '操作日志查看',
  [ALL_PERMISSIONS.LOG_DELETE]: '操作日志删除',
  [ALL_PERMISSIONS.LOG_EXPORT]: '日志导出',
  [ALL_PERMISSIONS.LOG_ANALYZE]: '日志分析',
  [ALL_PERMISSIONS.LOG_STATS_READ]: '日志统计查看',

  [ALL_PERMISSIONS.ADMIN_ACCESS]: '后台管理访问',
  [ALL_PERMISSIONS.ADMIN_DASHBOARD_READ]: '仪表板查看',
  [ALL_PERMISSIONS.ADMIN_REPORT_READ]: '报表查看',
  [ALL_PERMISSIONS.ADMIN_REPORT_EXPORT]: '报表导出',
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_READ]: '通知查看',
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_CREATE]: '通知创建',
  [ALL_PERMISSIONS.ADMIN_SETTING_READ]: '设置查看',
  [ALL_PERMISSIONS.ADMIN_SETTING_UPDATE]: '设置更新',

  [ALL_PERMISSIONS.PROFILE_READ]: '个人信息查看',
  [ALL_PERMISSIONS.PROFILE_UPDATE]: '个人信息编辑',
  [ALL_PERMISSIONS.PROFILE_RESET_PASSWORD]: '密码重置',
};

// 权限描述映射
export const PERMISSION_DESCRIPTIONS: Record<string, string> = {
  [ALL_PERMISSIONS.USER_LIST]: '查看用户列表',
  [ALL_PERMISSIONS.USER_READ]: '查看用户详细信息',
  [ALL_PERMISSIONS.USER_CREATE]: '创建新用户账户',
  [ALL_PERMISSIONS.USER_UPDATE]: '编辑用户信息',
  [ALL_PERMISSIONS.USER_DELETE]: '删除用户账户',
  [ALL_PERMISSIONS.USER_ENABLE]: '启用用户账户',
  [ALL_PERMISSIONS.USER_DISABLE]: '禁用用户账户',
  [ALL_PERMISSIONS.USER_RESET]: '重置用户密码',

  [ALL_PERMISSIONS.ROLE_READ]: '查看角色信息',
  [ALL_PERMISSIONS.ROLE_CREATE]: '创建新角色',
  [ALL_PERMISSIONS.ROLE_UPDATE]: '编辑角色信息',
  [ALL_PERMISSIONS.ROLE_DELETE]: '删除角色',
  [ALL_PERMISSIONS.ROLE_ENABLE]: '启用角色',
  [ALL_PERMISSIONS.ROLE_DISABLE]: '禁用角色',

  [ALL_PERMISSIONS.PERMISSION_READ]: '查看权限信息',
  [ALL_PERMISSIONS.PERMISSION_CREATE]: '创建新权限',
  [ALL_PERMISSIONS.PERMISSION_UPDATE]: '编辑权限信息',
  [ALL_PERMISSIONS.PERMISSION_DELETE]: '删除权限',
  [ALL_PERMISSIONS.PERMISSION_ENABLE]: '启用权限',
  [ALL_PERMISSIONS.PERMISSION_DISABLE]: '禁用权限',

  [ALL_PERMISSIONS.SYSTEM_CONFIG_READ]: '查看系统配置',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CREATE]: '创建系统配置',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_UPDATE]: '编辑系统配置',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_DELETE]: '删除系统配置',
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CONFIGURE]: '配置系统参数',
  [ALL_PERMISSIONS.SYSTEM_STATS_READ]: '查看系统统计',
  [ALL_PERMISSIONS.SYSTEM_STATS_ANALYZE]: '分析系统数据',
  [ALL_PERMISSIONS.SYSTEM_MONITOR]: '监控系统状态',
  [ALL_PERMISSIONS.SYSTEM_BACKUP]: '备份系统数据',
  [ALL_PERMISSIONS.SYSTEM_RESTORE]: '恢复系统数据',

  [ALL_PERMISSIONS.LOG_READ]: '查看操作日志',
  [ALL_PERMISSIONS.LOG_DELETE]: '删除操作日志',
  [ALL_PERMISSIONS.LOG_EXPORT]: '导出操作日志',
  [ALL_PERMISSIONS.LOG_ANALYZE]: '分析日志数据',
  [ALL_PERMISSIONS.LOG_STATS_READ]: '查看日志统计',

  [ALL_PERMISSIONS.ADMIN_ACCESS]: '访问后台管理系统',
  [ALL_PERMISSIONS.ADMIN_DASHBOARD_READ]: '查看管理仪表板',
  [ALL_PERMISSIONS.ADMIN_REPORT_READ]: '查看系统报表',
  [ALL_PERMISSIONS.ADMIN_REPORT_EXPORT]: '导出系统报表',
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_READ]: '查看系统通知',
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_CREATE]: '创建系统通知',
  [ALL_PERMISSIONS.ADMIN_SETTING_READ]: '查看系统设置',
  [ALL_PERMISSIONS.ADMIN_SETTING_UPDATE]: '更新系统设置',

  [ALL_PERMISSIONS.PROFILE_READ]: '查看个人信息',
  [ALL_PERMISSIONS.PROFILE_UPDATE]: '编辑个人信息',
  [ALL_PERMISSIONS.PROFILE_RESET_PASSWORD]: '重置个人密码',
};

// 权限模块映射
export const PERMISSION_MODULE_MAP: Record<string, PermissionModule> = {
  [ALL_PERMISSIONS.USER_LIST]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_READ]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_CREATE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_UPDATE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_DELETE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_ENABLE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_DISABLE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.USER_RESET]: PERMISSION_MODULES.USER,

  [ALL_PERMISSIONS.ROLE_READ]: PERMISSION_MODULES.ROLE,
  [ALL_PERMISSIONS.ROLE_CREATE]: PERMISSION_MODULES.ROLE,
  [ALL_PERMISSIONS.ROLE_UPDATE]: PERMISSION_MODULES.ROLE,
  [ALL_PERMISSIONS.ROLE_DELETE]: PERMISSION_MODULES.ROLE,
  [ALL_PERMISSIONS.ROLE_ENABLE]: PERMISSION_MODULES.ROLE,
  [ALL_PERMISSIONS.ROLE_DISABLE]: PERMISSION_MODULES.ROLE,

  [ALL_PERMISSIONS.PERMISSION_READ]: PERMISSION_MODULES.PERMISSION,
  [ALL_PERMISSIONS.PERMISSION_CREATE]: PERMISSION_MODULES.PERMISSION,
  [ALL_PERMISSIONS.PERMISSION_UPDATE]: PERMISSION_MODULES.PERMISSION,
  [ALL_PERMISSIONS.PERMISSION_DELETE]: PERMISSION_MODULES.PERMISSION,
  [ALL_PERMISSIONS.PERMISSION_ENABLE]: PERMISSION_MODULES.PERMISSION,
  [ALL_PERMISSIONS.PERMISSION_DISABLE]: PERMISSION_MODULES.PERMISSION,

  [ALL_PERMISSIONS.SYSTEM_CONFIG_READ]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CREATE]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_CONFIG_UPDATE]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_CONFIG_DELETE]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_CONFIG_CONFIGURE]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_STATS_READ]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_STATS_ANALYZE]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_MONITOR]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_BACKUP]: PERMISSION_MODULES.SYSTEM,
  [ALL_PERMISSIONS.SYSTEM_RESTORE]: PERMISSION_MODULES.SYSTEM,

  [ALL_PERMISSIONS.LOG_READ]: PERMISSION_MODULES.LOG,
  [ALL_PERMISSIONS.LOG_DELETE]: PERMISSION_MODULES.LOG,
  [ALL_PERMISSIONS.LOG_EXPORT]: PERMISSION_MODULES.LOG,
  [ALL_PERMISSIONS.LOG_ANALYZE]: PERMISSION_MODULES.LOG,
  [ALL_PERMISSIONS.LOG_STATS_READ]: PERMISSION_MODULES.LOG,

  [ALL_PERMISSIONS.ADMIN_ACCESS]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_DASHBOARD_READ]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_REPORT_READ]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_REPORT_EXPORT]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_READ]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_NOTIFICATION_CREATE]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_SETTING_READ]: PERMISSION_MODULES.ADMIN,
  [ALL_PERMISSIONS.ADMIN_SETTING_UPDATE]: PERMISSION_MODULES.ADMIN,

  [ALL_PERMISSIONS.PROFILE_READ]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.PROFILE_UPDATE]: PERMISSION_MODULES.USER,
  [ALL_PERMISSIONS.PROFILE_RESET_PASSWORD]: PERMISSION_MODULES.USER,
};

// 权限工具函数
export class PermissionUtils {
  /**
   * 检查用户是否具有指定权限
   */
  static hasPermission(userPermissions: string[], requiredPermission: string): boolean {
    return userPermissions.includes(requiredPermission);
  }

  /**
   * 检查用户是否具有指定权限列表中的任意一个
   */
  static hasAnyPermission(userPermissions: string[], requiredPermissions: string[]): boolean {
    return requiredPermissions.some(permission => userPermissions.includes(permission));
  }

  /**
   * 检查用户是否具有指定权限列表中的所有权限
   */
  static hasAllPermissions(userPermissions: string[], requiredPermissions: string[]): boolean {
    return requiredPermissions.every(permission => userPermissions.includes(permission));
  }

  /**
   * 获取权限名称
   */
  static getPermissionName(permission: string): string {
    return PERMISSION_NAMES[permission] || permission;
  }

  /**
   * 获取权限描述
   */
  static getPermissionDescription(permission: string): string {
    return PERMISSION_DESCRIPTIONS[permission] || permission;
  }

  /**
   * 获取权限所属模块
   */
  static getPermissionModule(permission: string): PermissionModule {
    return PERMISSION_MODULE_MAP[permission] || PERMISSION_MODULES.USER;
  }

  /**
   * 按模块分组权限
   */
  static groupPermissionsByModule(permissions: string[]): Record<PermissionModule, string[]> {
    const grouped: Record<PermissionModule, string[]> = {
      [PERMISSION_MODULES.USER]: [],
      [PERMISSION_MODULES.ROLE]: [],
      [PERMISSION_MODULES.PERMISSION]: [],
      [PERMISSION_MODULES.SYSTEM]: [],
      [PERMISSION_MODULES.LOG]: [],
      [PERMISSION_MODULES.ADMIN]: [],
    };

    permissions.forEach(permission => {
      const module = PERMISSION_MODULE_MAP[permission];
      if (module) {
        grouped[module].push(permission);
      }
    });

    return grouped;
  }

  /**
   * 获取所有权限列表
   */
  static getAllPermissions(): string[] {
    return Object.values(ALL_PERMISSIONS);
  }

  /**
   * 获取指定模块的所有权限
   */
  static getPermissionsByModule(module: PermissionModule): string[] {
    return this.getAllPermissions().filter(permission => 
      PERMISSION_MODULE_MAP[permission] === module
    );
  }

  /**
   * 解析权限代码
   */
  static parsePermissionCode(permissionCode: string): { resource: string; action: string } | null {
    const parts = permissionCode.split(':');
    if (parts.length === 2) {
      return {
        resource: parts[0],
        action: parts[1]
      };
    }
    return null;
  }

  /**
   * 生成权限代码
   */
  static generatePermissionCode(resource: string, action: string): string {
    return `${resource}:${action}`;
  }

  /**
   * 检查权限是否属于指定资源
   */
  static isPermissionForResource(permission: string, resource: string): boolean {
    const parsed = this.parsePermissionCode(permission);
    return parsed?.resource === resource;
  }

  /**
   * 检查权限是否属于指定操作
   */
  static isPermissionForAction(permission: string, action: string): boolean {
    const parsed = this.parsePermissionCode(permission);
    return parsed?.action === action;
  }

  /**
   * 获取指定资源的所有权限
   */
  static getPermissionsByResource(resource: string): string[] {
    return this.getAllPermissions().filter(permission => 
      this.isPermissionForResource(permission, resource)
    );
  }

  /**
   * 获取指定操作的所有权限
   */
  static getPermissionsByAction(action: string): string[] {
    return this.getAllPermissions().filter(permission => 
      this.isPermissionForAction(permission, action)
    );
  }
} 